from extensions import db, login_manager
from flask_login import UserMixin
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash
from flask import url_for

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, index=True)
    email = db.Column(db.String(120), unique=True, index=True)
    password_hash = db.Column(db.String(128))
    phone = db.Column(db.String(20))
    is_admin = db.Column(db.Boolean, default=False)
    is_member = db.Column(db.Boolean, default=False)
    member_since = db.Column(db.DateTime, nullable=True)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    bookings = db.relationship('Booking', backref='customer', lazy='dynamic')
    
    def set_password(self, password):
        self.password_hash = generate_password_hash(password)
        
    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

    def get_url(self):
        return url_for('main.course_detail', id=self.id)

class Coach(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64))
    introduction = db.Column(db.Text)
    photo = db.Column(db.String(128))
    experience = db.Column(db.Integer)  # 经验年数
    specialty = db.Column(db.String(128))  # 专长
    availability = db.Column(db.Boolean, default=True)
    bookings = db.relationship('Booking', backref='coach', lazy='dynamic')

class Course(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64))
    description = db.Column(db.Text)
    price = db.Column(db.Float)
    duration = db.Column(db.String(64))
    type = db.Column(db.String(64))  # 体验潜水、执证潜水、考证课程
    image = db.Column(db.String(128))
    bookings = db.relationship('Booking', backref='course', lazy='dynamic')

    def get_url(self):
        return url_for('main.course_detail', id=self.id)

class Booking(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    coach_id = db.Column(db.Integer, db.ForeignKey('coach.id'))
    course_id = db.Column(db.Integer, db.ForeignKey('course.id'))
    booking_date = db.Column(db.DateTime)
    status = db.Column(db.String(20), default='pending')  # pending, confirmed, completed, cancelled
    participants = db.Column(db.Integer, default=1)
    total_price = db.Column(db.Float)
    is_group = db.Column(db.Boolean, default=False)  # 是否是拼团
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    payment_status = db.Column(db.String(20), default='unpaid')  # unpaid, paid

class Activity(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    description = db.Column(db.Text, nullable=False)
    date = db.Column(db.DateTime, nullable=False)
    image = db.Column(db.String(100))
    price = db.Column(db.Float)
    location = db.Column(db.String(100))
    highlights = db.Column(db.Text)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(128))
    content = db.Column(db.Text)
    category = db.Column(db.String(64))  # 潜水知识、公司新闻等
    image = db.Column(db.String(128))
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

@login_manager.user_loader
def load_user(id):
    return User.query.get(int(id)) 